package com.frejoys.aop;

import com.frejoys.common.enums.EUserRole;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.util.CommonUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.dao.entity.User;
import com.frejoys.service.UserService;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Component
@RequiredArgsConstructor
@Aspect
public class VerifyStoreAop {

    private final UserService userService;

    @Pointcut("@annotation(com.frejoys.common.aop.VerifyStore)")
    public void atestAop() {}

    @Around("atestAop()")
    public Object doAround(ProceedingJoinPoint jp) throws Throwable {

        User user = userService.getInfo(RequestUtil.userId());
        if (!CommonUtil.bitExistOffset(user.getRole(), EUserRole.store.getValue())) {
           throw new GeneralException(HttpStatus.sotreRoleNoExist);
        }

        return jp.proceed();
    }


}
